<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
if (!ispost())
{
    exit();
}
if (!isset($_SESSION['crtime']))
{
    $_SESSION['crtime'] = systime();
}
else if ($_SESSION['crtime'] == systime())
{
    exit();
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chk = new CheckMode_3d($curruser['acl'], 3, $gametype);
$chk->checkShow3dPage();
$show3D3S = $chk->show3D3S;
$g_betname_3or5 = judge_type($gametype);
unset($chk);
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
require_once( "class_3d/Bet_3d.php" );
require_once( "class_3d/RateConfig_3d.php" );
require_once( "class_3d/SRate_3d.php" );
require_once( "class_3d/MyLotto_3d.php" );
require_once( "class_3d/Amounts_3d.php" );
include( "class_3d/class.lotto3d.php" );
$lotto3d = new lotto3d($gametype);
$ui = new UI_3D(true);
$tpl = new Template2();
$conf = new Config();
$RateConfig_3d = new RateConfig_3d($gametype);
$SRate_3d = new SRate_3d($gametype);
$MyLotto_3d = new MyLotto_3d($gametype);
$Amounts_3d = new Amounts_3d($gametype);
//2013-11-20
$Bet_3d = new Bet_3s($gametype);
$amountField = "balance";
/*
switch ($show3D3S)
{
    case 0 :
        $Bet_3d = new Bet_3d($gametype);
        $amountField = "amount_3d";
        break;
    case 1 :
        $Bet_3d = new Bet_3s($gametype);
        $amountField = "amount_3s1";
        if ($curruser['amount_type'] && 0 < $curruser[$amountField])
        {
            $amountFloat = min(0, $Bet_3d->getAmountFloat($curruser['id']));
            $curruser[$amountField] += $amountFloat;
        }
        break;
}*/
$today = date("Y-m-d");
$bettypegroup = getvar("palygroup");
$betmin = $conf->get("betmin_{$tbl}");
$setMomney = intval(getvar("setMomney"));
$rcmp3str = htmlspecialchars($_POST['rcmp3str'], ENT_QUOTES);
$rcmp2str = htmlspecialchars($_POST['rcmp2str'], ENT_QUOTES);
$rcmp1str = htmlspecialchars($_POST['rcmp1str'], ENT_QUOTES);
$oldrate = floatval($_POST['oldrate']);
$rcmp3 = explode(",", $rcmp3str);
$rcmp2 = explode(",", $rcmp2str);
$rcmp1 = explode(",", $rcmp1str);
$disk = intval(getvar("disk"));
if (!isset($curruser["disk{$disk}"]) || $curruser["disk{$disk}"] != 1)
{
    $ui->showErrorPage("盘类错误");
    exit();
}
$diskStr = $RateConfig_3d->diskArr[$disk];
$diskStr2 = $g_disk_name[$disk];
unset($_POST['disk']);
if (is_array($rcmp3) && is_array($rcmp2) && is_array($rcmp1))
{
    $chkNum3 = count($rcmp3);
    $chkNum2 = count($rcmp2);
    $chkNum1 = count($rcmp1);
    $totalChkNum = $chkNum3 + $chkNum2 + $chkNum1;
    if ($chkNum3 < 1 || $chkNum2 < 1 || $chkNum1 < 1)
    {
        $ui->showErrorPage("每位数至少勾选一个号码");
        exit();
    }
    else
    {
        if (10 < $chkNum3 || 10 < $chkNum2 || 10 < $chkNum1)
        {
            $ui->showErrorPage("每位数最多勾选10个号码");
            exit();
        }
        else
        {
            if ($totalChkNum < 9)
            {
                $ui->showErrorPage("勾选的总号码量至少要9个");
                exit();
            }
            else
            {
                if (21 < $totalChkNum)
                {
                    $ui->showErrorPage("勾选的总号码量不可超过21个");
                    exit();
                }
            }
        }
    }
}
else
{
    $ui->showErrorPage("资料不齐全，无法下注");
    exit();
}
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("帐号已被暂停使用");
    exit();
}
else if (1 < $curruser['acl'])
{
    $ui->showErrorPage("非会员无法下注");
    exit();
}
else if ($bettypegroup != "rcmp")
{
    $ui->showErrorPage("不正确的玩法页面");
    exit();
}
else if ($setMomney < $betmin)
{
    $ui->showErrorPage("下注金额不能小於最低限额({$betmin}元)");
    exit();
}
include_once( "BetChk.php" );
$BetChk = new BetChk($today, $gametype);
if ($lotto_3d = $BetChk->chkBetDate($today))
{
    $lotto_id = $lotto_3d['id'];
    $round = $lotto_3d['round'];
    $paperOpen = $lotto_3d['open'];
    $betSubIdx = "35_0";
    if (!isset($g_betname_3or5[$betSubIdx]))
    {
        $betOKFlag = false;
        $timeTemp = rand(10, 99) . "1" . rand(0, 9);
        $errMsg = "({$tbl})[betrcmpdo]No.{$timeTemp} 会员下注:不被许\可的玩法:id-35, subid-0";
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
        $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
        exit();
    }
    if (0 < $curruser[$amountField])
    {
        $winMoney = $Bet_3d->getSumWinMoney($curruser['id'], $lotto_id, $paperOpen);
        $curruser[$amountField] += $winMoney; ;
    }
    $bettype = "35";
    $chkNumFlag = $RateConfig_3d->chkRcmpNum($rcmp3);
    if ($chkNumFlag !== false)
    {
        $betOKFlag = false;
        $timeTemp = rand(10, 99) . "2" . rand(0, 9);
        $errMsg = "({$tbl})[betrcmpdo]No.{$timeTemp} 会员下注RCMP:不正确的内容:id-" . $bettype . ",num3-" . $chkNumFlag;
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
        $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
        exit();
        break;
    }
    $chkNumFlag = $RateConfig_3d->chkRcmpNum($rcmp2);
    if ($chkNumFlag !== false)
    {
        $betOKFlag = false;
        $timeTemp = rand(10, 99) . "3" . rand(0, 9);
        $errMsg = "({$tbl})[betrcmpdo]No.{$timeTemp}会员下注RCMP:不正确的内容:id-" . $bettype . ",num2-" . $chkNumFlag;
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
        $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
        exit();
        break;
    }
    $chkNumFlag = $RateConfig_3d->chkRcmpNum($rcmp1);
    if ($chkNumFlag !== false)
    {
        $betOKFlag = false;
        $timeTemp = rand(10, 99) . "4" . rand(0, 9);
        $errMsg = "({$tbl})[betrcmpdo]No.{$timeTemp}会员下注RCMP:不正确的内容:id-" . $bettype . ",num1-" . $chkNumFlag;
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
        $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
        exit();
        break;
    }
    $rcmpTempStr = "佰位:" . $rcmp3str . " 拾位:" . $rcmp2str . " 个位:" . $rcmp1str;
    $amountsData = $Amounts_3d->getAmountsDataById($bettype, $curruser['id']);
    //$betOKFlag = false;
    //$errMsg = "({$tbl})会员下注:无该玩法之下注限额资料:玩法id-" . $bettype;
    //write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
    //$ui->showErrorPage("无限额资料,无法下注");
    //exit();
    $amount1 = $amountsData['amount1'];
    $amount2 = $amountsData['amount2'];
    $amount1 = $amount1 ? $amount1 : "0";
    $amount2 = $amount2 ? $amount2 : "0";
    $usedamount = $Bet_3d->getSumAmount($curruser['id'], $lotto_id, $paperOpen);
    $usedamount = $usedamount ? $usedamount : "0";
    if ($setMomney < $betmin)
    {
        $betOKFlag = false;
        $ui->showErrorPage("下注金额不能小於最低限额:" . $betmin);
        exit();
    }
    else if ($curruser[$amountField] < $setMomney + $usedamount)
    {
        $betOKFlag = false;
        $ui->showErrorPage("下注金额不能超过信用额度");
        exit();
    }
    else if ($curruser['usemaxamount'] && $curruser['maxamount'] < $setMomney + $usedamount)
    {
        $betOKFlag = false;
        $ui->showErrorPage("下注金额不能超过本期限额");
        exit();
    }
    else if ($amount1 < $setMomney)
    {
        $betOKFlag = false;
        $ui->showErrorPage("下注金额不能超过单注限额");
        exit();
    }
    $tempRateBaseArr = $SRate_3d->getRates($lotto_id, $disk, $bettype);
    if ($tempRateBaseArr == false)
    {
        $betOKFlag = false;
        $errMsg = "({$tbl})会员下注RCMP:无赔率资料:id-" . $bettype;
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
        $ui->showErrorPage("查无复式组合的赔率资料，无法下注。若有任何问题请洽管理员。<br />错误讯息：" . $errMsg);
        exit();
    }
    $user_id = $SRate_3d->getAcl5ID($curruser['id'], $curruser['acl']);
    if ($tempRateBaseArr2 = $SRate_3d->getRatesS($lotto_id, $bettype, $user_id))
    {
        $tempRateArr = array();
        foreach ($tempRateBaseArr as $key2 => $val2)
        {
            $tempRateArr[$key2] = $tempRateBaseArr[$key2] - $tempRateBaseArr2[$key2];
        }
        $rateArr = $tempRateArr;
        $rate5 = 0;
        $rate5Flag = 1;
    }
    else
    {
        $rateArr = $tempRateBaseArr;
        $rate5 = 0;
        $rate5Flag = 0;
    }
    sort($rcmp3);
    sort($rcmp2);
    sort($rcmp1);
    $checkArr = array();
    foreach ($rcmp3 as $val3)
    {
        foreach ($rcmp2 as $val2)
        {
            foreach ($rcmp1 as $val1)
            {
                $checkArr[] = $val3 . $val2 . $val1 . "";
            }
        }
    }
    $totalRate = 0;
    $totalNum = 0;
    $totalRate5 = 0;
    $u2 = get_user($curruser['prev_id'], $gametype);
    $u3 = get_user($u2['prev_id'], $gametype);
    $u4 = get_user($u3['prev_id'], $gametype);
    $u5 = get_user($u4['prev_id'], $gametype);
    $mpercent = $u2['mpercent'];
    $percent2 = $u2['percent0'];
    $percent3 = min($u3['percent1'], $u4['mpercent'] - $mpercent - $percent2);
    $percent4 = min($u4['percent1'], $u5['mpercent'] - $mpercent - $percent2 - $percent3);
    $upercent = 100 - $mpercent - $percent2 - $percent3 - $percent4;
    $fields = array();
    $fields['percent2'] = $mpercent;
    $fields['percent3'] = $percent2;
    $fields['percent4'] = $percent3;
    $fields['percent5'] = $percent4;
    $fields['percent255'] = $upercent;
    $check_percent = $fields['percent255'];
    $fields['acl2'] = $u2['id'];
    $fields['acl3'] = $u3['id'];
    $fields['acl4'] = $u4['id'];
    $fields['acl5'] = $u5['id'];
    $getRatesNumArr = $SRate_3d->getRatesNumArr($lotto_id, $disk, $bettype, 0);
    $getRatesSNumArr = $SRate_3d->getRatesSNumArr($lotto_id, $user_id, $bettype, 0);
    foreach ($checkArr as $val)
    {
        $rate = $RateConfig_3d->getRateFromRateArr($bettype, $val, $rateArr, $getRatesNumArr, $getRatesSNumArr);
        if ($rate <= 1)
        {
            $betOKFlag = false;
            $errMsg = "({$tbl})会员下注RCMP:赔率值错误:id-" . $bettype . ",num-" . $val . ",rate-" . $rate;
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            $ui->showErrorPage("复式组合所勾选号码的组合中出现了错误的赔率值，无法下注。若有任何问题请洽管理员。");
            exit();
        }
        $totalRate += $rate;
        ++$totalNum;
        if ($rate5Flag)
        {
            $totalRate5 += $RateConfig_3d->getRateFromRateArr($bettype, $val, $tempRateBaseArr);
        }
    }
    $rate = $totalRate / $totalNum / ( $chkNum3 * $chkNum2 * $chkNum1 );
    if ($rate5Flag)
    {
        $rate5 = $totalRate5 / $totalNum / ( $chkNum3 * $chkNum2 * $chkNum1 ) - $rate;
    }
    else
    {
        $rate5 = 0;
    }
    $ret = $Amounts_3d->check_Amount_255($lotto_id, $bettype, $checkArr, $setMomney * $upercent / 100);
    if (count($ret))
    {
        $betOKFlag = false;
        $errMsg = "下注号码:" . implode(",", $ret) . ",总额度已满.";
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
        $ui->showErrorPage("复式组合所勾选号码的组合中总额度已满，无法下注。");
        exit();
    }
    $rcmp3Str = implode("", $rcmp3);
    $rcmp2Str = implode("", $rcmp2);
    $rcmp1Str = implode("", $rcmp1);
    $fieldsRcmp2 = "( {$rcmp3Str} x {$rcmp2Str} x {$rcmp1Str} )";
    if ($rate <= 1)
    {
        $betOKFlag = false;
        $errMsg = "({$tbl})会员下注RCMP:赔率值错误:id-{$bettype},num-佰({$rcmp3Str});拾({$rcmp2Str});个({$rcmp3Str}),rate-" . $rate;
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
        $showBetTable[] = array("bettype" => $g_betname_3d['35'], "nums" => $fieldsRcmp2, "rate" => $rate, "betmoney" => $setMomney, "betflag" => false, "note" => "赔率值小於1，无法下注。");
    }
    else
    {
        $rate = round($rate, 2);
        $fieldsRcmp = implode("", $rcmp3) . "," . implode("", $rcmp2) . "," . implode("", $rcmp1);
        $fields['user_id'] = $curruser['id'];
        $fields['lotto_id'] = $lotto_id;
        $fields['bettype'] = "35";
        $fields['numbers'] = "'" . $fieldsRcmp . "'";
        $fields['amount'] = $setMomney;
        $fields['rate'] = $rate;
        $fields['rate5'] = $rate5;
        $fields['payoff'] = $setMomney * ( $rate - 1 );
        $fields['disk'] = $disk;
        $fields['line_nums'] = "'" . $fieldsRcmp2 . "'";
        $fields['unitmoney'] = $setMomney;
        $fields['rame'] = "0";
        $fields['State'] = "0";
        $fields['ip'] = "'" . getIP() . "'";
        $fields['back1'] = floatval($Amounts_3d->getBackArr("35", $curruser['id'], $disk));
        $fields['back2'] = floatval($Amounts_3d->getBackArr("35", $u2['id'], $disk));
        $fields['back3'] = floatval($Amounts_3d->getBackArr("35", $u3['id'], $disk));
        $fields['back4'] = floatval($Amounts_3d->getBackArr("35", $u4['id'], $disk));
        $fields['back5'] = floatval($Amounts_3d->getBackArr("35", $u5['id'], $disk));
        if ($u5['supe_account'] != "")
        {
            $rs = getdb("select id from k_user where acl=255 and account='" . $u5['supe_account'] . "'");
            $uidq = $rs->f['id'];
            $rs->close();
            $fields['back6'] = $Amounts_3d->getBackArr("35", $uidq, $disk);
        }
        else
        {
            $fields['back6'] = "0";
        }
        if ($fields['percent255'] < 0)
        {
            $ui->showErrorPage("系统参数严重错误！");
            exit();
        }
        $fields['r2'] = $val * $fields['percent3'] / 100;
        $fields['r3'] = $val * $fields['percent4'] / 100;
        $fields['r4'] = $val * $fields['percent5'] / 100;
        $fields['r6'] = $val * $fields['percent255'] / 100;
        $fields['createtime'] = systime();
        $fields['sessid'] = "'" . enBetSessid($fields) . "'";
        $_SESSION['crtime'] = $fields['createtime'];
        $numberamount = $Bet_3d->getSumBettypeAmount($curruser['id'], $lotto_id, $bettype);
        if ($amount2 < $setMomney + $numberamount)
        {
            $betOKFlag = false;
            $ui->showErrorPage("下注金额不能超过单项限额");
            exit();
        }
        $insertFlag = $Bet_3d->dbInsert($fields);
        $note = "";
        $insertId = "";
        if ($insertFlag == false)
        {
            $note = "<font color=\"red\">下注失败</font>";
        }
        else if ($oldrate != "" && $oldrate != $rate)
        {
            $note = "<font color=\"red\">赔率已更新</font>";
            $insertId = $insertFlag;
            $insertFlag = true;
        }
        else
        {
            $note = "";
            $insertId = $insertFlag;
            $insertFlag = true;
        }
        $showBetTable[] = array("bettype" => $g_betname_3d['35'], "nums" => $fieldsRcmp2, "rate" => $rate, "betmoney" => $setMomney, "betflag" => $insertFlag, "note" => $note);
        if ($insertId !== "")
        {
            $lotto3d->getBet_3D($insertId);
        }
    }
    $ui->header();
    echo "<table class=\"tblMy3D\">";
    echo "<thead><tr class=\"title0 bgImg_" . $gametype . "\"><th colspan=7>" . $tblName . " 第" . $round . "期 您下注的是：</th></tr></thead>";
    echo "<thead><tr class=\"bgClc_" . $gametype . "\"><th>下注方式</th><th>下注内容</th><th>盘类</th><th>赔率</th><th>下注金额</th><th>下注结果</th><th>说明</th></tr></thead>";
    echo "<tbody>";
    foreach ($showBetTable as $val)
    {
        if ($val['betflag'])
        {
            $tdMsg = "<font color=green>成功</font>";
            $classTemp = "td2";
        }
        else
        {
            $tdMsg = "失败";
            $classTemp = "red";
        }
        echo "<tr class=\"" . $classTemp . "\">";
        echo "<td>" . $val['bettype'] . "</td>";
        echo "<td>" . nl2br($val['nums']) . "</td>";
        echo "<td>" . $diskStr2 . "</td>";
        echo "<td>" . $val['rate'] . "</td>";
        echo "<td align=\"right\">" . $val['betmoney'] . "</td>";
        echo "<td>";
        echo $tdMsg;
        echo "</td>";
        echo "<td>" . $val['note'] . "</td>";
        echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>";
    $timebtnType = getvar("timebtnType") == "checkbox" ? "checkbox" : "text";
    echo "<script>";
    echo "parent.leftFrame.location.replace('op.php?op=member_3d&fp=account&gametype={$gametype}');";
    echo "setTimeout(\"window.top.mainFrame.location.replace('op.php?op=member_3d&fp=bet&palygroup={$bettypegroup}&timebtnType={$timebtnType}&gametype={$gametype}&disk={$disk}');\",2000);";
    echo "</script>";
    $ui->footer();
}
?>